package icraf.common.gui;

import java.awt.Color;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.table.JTableHeader;

public class TableRowHeaderRenderer extends JLabel implements
		ListCellRenderer<Object> {
	private static final long serialVersionUID = 4342965157801058728L;
	private boolean isTotalFields = false;
	private JTable table;
	private Color backgroundTotal = new Color(150, 150, 255);
	private Color highlightColor = new Color(240, 210, 100);

	// private Dimension minimumHeaderSize = new Dimension(150,20);

	/**
	 * Constructor creates all cells the same To change look for individual
	 * cells put code in getListCellRendererComponent method
	 **/
	public TableRowHeaderRenderer(JTable table) {
		this.table = table;
		JTableHeader header = table.getTableHeader();
		setOpaque(true);
		// setBorder(UIManager.getBorder("TableHeader.cellBorder"));
		setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder(""),
				BorderFactory.createEmptyBorder(5, 5, 5, 5)));
		setHorizontalAlignment(RIGHT);
		setForeground(header.getForeground());
		setBackground(header.getBackground());
		setFont(header.getFont());

	}

	/**
	 * Returns the JLabel after setting the text of the cell
	 **/
	public Component getListCellRendererComponent(JList<?> list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		setText((value == null) ? "" : value.toString());
		if (isSelected) {
			setBackground(highlightColor);
		} else {
			setBackground(table.getTableHeader().getBackground());
		}
		if (isTotalFields) {
			if (value.equals("TOTAL")) {
				setBackground(backgroundTotal);
			}
		}

		return this;
	}

	public void setTotalFields(boolean b) {
		this.isTotalFields = b;
	}

}
